package com.baomidou.ant.service.impl;

import com.baomidou.ant.entity.ApplyFamilyInfo;
import com.baomidou.ant.entity.ApplyInfo;
import com.baomidou.ant.entity.ApplyInternshipInfo;
import com.baomidou.ant.entity.ApplyTrainInfo;
import com.baomidou.ant.mapper.ApplyInfoMapper;
import com.baomidou.ant.service.IApplyFamilyInfoService;
import com.baomidou.ant.service.IApplyInfoService;
import com.baomidou.ant.service.IApplyInternshipInfoService;
import com.baomidou.ant.service.IApplyTrainInfoService;
import com.baomidou.ant.vo.request.*;
import com.baomidou.ant.vo.response.ResponseListApplyInfoVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author jobob
 * @since 2022-09-20
 */
@Service
public class ApplyInfoServiceImpl extends ServiceImpl<ApplyInfoMapper, ApplyInfo> implements IApplyInfoService {
    @Autowired
    IApplyFamilyInfoService familyInfoService;
    @Autowired
    IApplyInternshipInfoService internshipInfoService;
    @Autowired
    IApplyTrainInfoService trainInfoService;

    @Override
    public boolean saveApply(RequestFillingApplyInfoVo vo) {
        ApplyInfo applyInfo=new ApplyInfo();
        BeanUtils.copyProperties(vo,applyInfo);
        applyInfo.setTPolitical(vo.getThisPolitical());
        if(save(applyInfo)){
            int applyId=applyInfo.getId();
            List<RequestInternshipInfoVo> internshipInfoVos=vo.getInternshipInfoVos();
            List<RequestTrainInfoVo> trainInfoVos=vo.getTrainInfoVos();
            List<RequestFamilyInfoVo> familyInfoVos=vo.getFamilyInfoVos();
            for(RequestInternshipInfoVo internshipInfoVo:internshipInfoVos){
                if(ObjectUtils.isEmpty(internshipInfoVo.getCompanyName())
                        &&ObjectUtils.isEmpty(internshipInfoVo.getEndTime())
                        &&ObjectUtils.isEmpty(internshipInfoVo.getPostName())
                        &&ObjectUtils.isEmpty(internshipInfoVo.getStartTime())){
                    continue;
                }else {
                    ApplyInternshipInfo applyInternshipInfo=new ApplyInternshipInfo();
                    BeanUtils.copyProperties(internshipInfoVo,applyInternshipInfo);
                    applyInternshipInfo.setApplyInfoId(applyId);
                    internshipInfoService.save(applyInternshipInfo);
                }
            }
            for(RequestTrainInfoVo trainInfoVo:trainInfoVos){
                if(ObjectUtils.isEmpty(trainInfoVo.getTrainTime())
                        &&ObjectUtils.isEmpty(trainInfoVo.getTrainInfo())){
                    continue;
                }else {
                    ApplyTrainInfo applyTrainInfo=new ApplyTrainInfo();
                    BeanUtils.copyProperties(trainInfoVo,applyTrainInfo);
                    applyTrainInfo.setApplyInfoId(applyId);
                    trainInfoService.save(applyTrainInfo);
                }
            }
            for(RequestFamilyInfoVo familyInfoVo:familyInfoVos){
                if(ObjectUtils.isEmpty(familyInfoVo.getAppellation())
                        &&ObjectUtils.isEmpty(familyInfoVo.getName())
                        &&ObjectUtils.isEmpty(familyInfoVo.getNowLive())
                        &&ObjectUtils.isEmpty(familyInfoVo.getNowWorkplace())){
                    continue;
                }else {
                    ApplyFamilyInfo applyFamilyInfo=new ApplyFamilyInfo();
                    BeanUtils.copyProperties(familyInfoVo,applyFamilyInfo);
                    applyFamilyInfo.setApplyInfoId(applyId);
                    familyInfoService.save(applyFamilyInfo);
                }
            }
            return true;
        }else
            return false;
    }

    @Override
    public ApplyInfo queryByUser(String userTel) {
        QueryWrapper<ApplyInfo> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("telephone",userTel);
        return getOne(queryWrapper);
    }

    @Override
    public IPage<ResponseListApplyInfoVo> listApplyInfo(RequestListApplyInfoVo vo) {
        IPage<ApplyInfo> iPage=new Page<>(vo.getDisplayStart(),vo.getDisplayLength());
        QueryWrapper<ApplyInfo> queryWrapper=new QueryWrapper<>();
        if(ObjectUtils.isNotEmpty(vo.getApplyUserName_Filling())){
            queryWrapper.like("apply_user_name",vo.getApplyUserName_Filling());
        }
        if (ObjectUtils.isNotEmpty(vo.getIntentionPost_Filling())){
            queryWrapper.like("intention_post",vo.getIntentionPost_Filling());
        }
        if(ObjectUtils.isNotEmpty(vo.getTelephone_Filling())){
            queryWrapper.like("telephone",vo.getTelephone_Filling());
        }
        queryWrapper.orderByDesc("create_time");
        int allTotal=list(queryWrapper).size();
        IPage<ApplyInfo> applyIPage=page(iPage,queryWrapper);
        return applyIPage.convert(applyInfo -> {
            ResponseListApplyInfoVo responseListApplyInfoVo=new ResponseListApplyInfoVo();
            BeanUtils.copyProperties(applyInfo,responseListApplyInfoVo);
            responseListApplyInfoVo.setAllTotal(allTotal);
            return responseListApplyInfoVo;
        });
    }
}
